#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include <mh/common.h>
#include <mh/com.h>
#include "resource.h"
#include "func.h"
#include "mhctrlstate.h"
#include "lang.h"

/*
 * get string of command
 */
void GetCmdString(char *sc, int cmd, int a0h1)
{
    sc[0] = 0;
    switch(cmd)
    {
    case MH_ACT_NONE:
        break;
    case MH_ACT_TRANSFER:
        if(!a0h1)
        {
            strcat(sc, MW_TRANSFER_CAPTION);
        }
        break;
    case MH_ACT_RECEIVE:
        if(a0h1)
        {
            strcat(sc, MW_RECEIVE_CAPTION);
        }
        break;
    case MH_ACT_TRAN_RECV:
        if(a0h1)
        {
            strcat(sc, MW_RECEIVE_CAPTION);
        }
        else
        {
            strcat(sc, MW_TRANSFER_CAPTION);
        }
        break;
    case MH_ACT_KNIT_RECV:
        if(a0h1)
        {
            strcat(sc, MW_RECEIVE_CAPTION);
        }
        else
        {
            strcat(sc, MW_KNIT_CAPTION);
        }
        break;
    case MH_ACT_KNIT:
        if(!a0h1)
        {
            strcat(sc, MW_KNIT_CAPTION);
        }
        break;
    case MH_ACT_TUCK:
        if(a0h1)
        {
            strcat(sc, MW_TUCK_CAPTION);
        }
        break;
    case MH_ACT_RUDE:
        break;
    case MH_ACT_KNIT_RUDE:
        break;
    case MH_ACT_KNIT_TUCK:
        if(a0h1)
        {
            strcat(sc, MW_TUCK_CAPTION);
        }
        else
        {
            strcat(sc, MW_KNIT_CAPTION);
        }
        break;
    case MH_ACT_KNIT_TRAN:
        if(!a0h1)
        {
            strcat(sc, MW_KNIT_TRAN_CAPTION);
        }
        break;
    case MH_ACT_2RECV:
        if(a0h1)
        {
            strcat(sc, MW_RECV2_CAPTION);
        }
        break;
    case MH_ACT_TRAN_2RECV:
        if(a0h1)
        {
            strcat(sc, MW_RECV2_CAPTION);
        }
        else
        {
            strcat(sc, MW_TRANSFER_CAPTION);
        }
        break;
    case MH_ACT_2TUCK:
        if(a0h1)
        {
            strcat(sc, MW_TUCK2_CAPTION);
        }
        break;
    case MH_ACT_KNIT_2TUCK:
        if(a0h1)
        {
            strcat(sc, MW_TUCK2_CAPTION);
        }
        else
        {
            strcat(sc, MW_KNIT_CAPTION);
        }
        break;
    case MH_ACT_JI:
        if(a0h1)
        {
            strcat(sc, MW_JI_CAPTION);
        }
        break;
    default:
        break;
    }
}
/*
 * get string of code
 */
void GetCodeString(char *sc, ushort code)
{
    char *str[16] = {
        "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"
    };
    sc[0] = 0;
    int i = 0;
    for(i = 1; i < 16; ++i)
    {
        if(code & (1 << i))
        {
            if(sc[0])
            {
                strcat(sc, " ");
            }
            strcat(sc, str[i]);
        }
    }
}
/*
 * get string of yarn
 */
void GetYarnString(char *sc, ushort yarn, int exchange)
{
    char *str[16] = {
        "1", "2", "3", "4", "5", "6", "7", "8",
        "11", "12", "13", "14", "15", "16", "17", "18"
    };
    sc[0] = 0;
    int i = 0;
    for(i = 0; i < 16; ++i)
    {
        if(yarn & (0x8000 >> i))
        {
            if(sc[0])
            {
                strcat(sc, " ");
            }
            uchar uc = 0;
            if(exchange)
            {
                uc = GetExchangedYarn(g_knit_data, 0, i);
            }
            if(uc)
            {
                sprintf(&sc[strlen(sc)], "%d", uc);
            }
            else
            {
                strcat(sc, str[i]);
            }
            uc = 0;
            if(exchange)
            {
                uc = GetExchangedYarn(g_knit_data, 1, i);
            }
            if(uc)
            {
                strcat(sc, " ");
                sprintf(&sc[strlen(sc)], "%d", uc);
            }
        }
    }
}
/*
 * get string of rock
 */
void GetRockString(char *sc, int drt, int needle, int flag)
{
    switch(drt)
    {
    case ROCK_LEFT:
        sc[0] = 'L';
        break;
    default:
        sc[0] = 'R';
        break;
    }
    sprintf(&sc[1], "%u", needle);
    if(flag == MH_ROCK_STAR_POS)
    {
        strcat(sc, "*");
    }
    else if(flag == MH_ROCK_PLUS_POS)
    {
        strcat(sc, "+");
    }
    else if(flag == MH_ROCK_DEC_POS)
    {
        strcat(sc, "-");
    }
    else if(flag == MH_ROCK_WELL_POS)
    {
        strcat(sc, "#");
    }
}
/*
 *
 */
void Truncate(char *out, char *in, uint len)
{
    int i = 0;
    while(i < len)
    {
        if(!in[i])
        {
            while(i < len)
            {
                out[i] = ' ';
                ++i;
            }
            break;
        }
        out[i] = in[i];
        if(in[i] & 0x80)//chinese
        {
            if(i == len - 1)
            {
                out[i] = ' ';
                break;
            }
            else if(i == len - 2)
            {
                out[i] = ' ';
                out[i + 1] = ' ';
                break;
            }
            ++i;
            out[i] = in[i];
        }
        else
        {
            if(i == len - 1)
            {
                out[i] = ' ';
            }
        }
        ++i;
    }
}
/*
 * check usb
 */
int HasUsb()
{
    int ret = FALSE;
    system("mount | grep udisk > /tmp/1.txt");
    FILE *fp = fopen("/tmp/1.txt", "r");
    char sc[200] = {0};
    if(fp)
    {
        if ((fgets (sc, 200, fp)) != NULL)
        {
            ret = 1;
        }
        fclose(fp);
    }
    remove("/tmp/1.txt");

    /*
    int ret = FALSE;
    struct stat buf;
    if(!stat("/udisk/", &buf))
    {
        ret = TRUE;
    }*/
    return ret;
}
/*
 *
 */
void GetCntInfo(void *p, ushort flw_line)
{
    CNT_INFO *pinfo = (CNT_INFO*)p;
    ushort total = GetTotalKnitPage(g_knit_data);
    KNIT_PAGE *pcon = GetControlData(g_knit_data);
    int i = 0;
    for(i = 0; i < total; ++i)
    {
        ushort n = pcon[i].flw_1F;
        if(n == flw_line)
        {
            pinfo->height = i + 1;
            pinfo->act = pcon[i].act_1FA;
            pinfo->codeA = pcon[i].code_1FA;
            pinfo->codeH = pcon[i].code_1FH;
            pinfo->yarn = (pcon[i].gauze1_1 << 8) | pcon[i].gauze1_9;
            break;
        }
        n = pcon[i].flw_1B;
        if(n == flw_line)
        {
            pinfo->height = i + 1;
            pinfo->act = pcon[i].act_1BA;
            pinfo->codeA = pcon[i].code_1BA;
            pinfo->codeH = pcon[i].code_1BH;
            pinfo->yarn = (pcon[i].gauze1_1 << 8) | pcon[i].gauze1_9;
            break;
        }
        n = pcon[i].flw_2F;
        if(n == flw_line)
        {
            pinfo->height = i + 1;
            pinfo->act = pcon[i].act_2FA;
            pinfo->codeA = pcon[i].code_2FA;
            pinfo->codeH = pcon[i].code_2FH;
            pinfo->yarn = (pcon[i].gauze2_1 << 8) | pcon[i].gauze2_9;
            break;
        }
        n = pcon[i].flw_2B;
        if(n == flw_line)
        {
            pinfo->height = i + 1;
            pinfo->act = pcon[i].act_2BA;
            pinfo->codeA = pcon[i].code_2BA;
            pinfo->codeH = pcon[i].code_2BH;
            pinfo->yarn = (pcon[i].gauze2_1 << 8) | pcon[i].gauze2_9;
            break;
        }

        uchar *r = (uchar*)&pcon[i];
        if ((r[113] == 0) && (r[117] == 0))
        {
            continue;
        }

        n = pcon[i].flw_3F;
        if(n == flw_line)
        {
            pinfo->height = i + 1;
            pinfo->act = pcon[i].act_3FA;
            pinfo->codeA = pcon[i].code_3FA;
            pinfo->codeH = pcon[i].code_3FH;
            pinfo->yarn = 0;
            break;
        }
        n = pcon[i].flw_3B;
        if(n == flw_line)
        {
            pinfo->height = i + 1;
            pinfo->act = pcon[i].act_3BA;
            pinfo->codeA = pcon[i].code_3BA;
            pinfo->codeH = pcon[i].code_3BH;
            pinfo->yarn = 0;
            break;
        }
        n = pcon[i].flw_4F;
        if(n == flw_line)
        {
            pinfo->height = i + 1;
            pinfo->act = pcon[i].act_4FA;
            pinfo->codeA = pcon[i].code_4FA;
            pinfo->codeH = pcon[i].code_4FH;
            pinfo->yarn = 0;
            break;
        }
        n = pcon[i].flw_4B;
        if(n == flw_line)
        {
            pinfo->height = i + 1;
            pinfo->act = pcon[i].act_4BA;
            pinfo->codeA = pcon[i].code_4BA;
            pinfo->codeH = pcon[i].code_4BH;
            pinfo->yarn = 0;
            break;
        }
    }
}
/** \brief  :获取纱嘴编号，如果有多个纱嘴，只返回第一个检测到的纱嘴，从高位往低位检测
 *
 *  \param  :gauze,16bit,bit15为1号纱嘴，bit0为16号纱嘴
 *  \return :gauze num
 *  \author :haoyaoyao
 *  \date   :2013\11\28
 */
static uchar GetGauzeValue(ushort gauze)
{
    uchar ret;
    int i;
    for(i = 0; i < 16; i++)
    {
        if(gauze & (0x8000 >> i))
        {
            uchar uc = GetExchangedYarn(g_knit_data, 0, i);
            if(uc)
            {
                ret = uc;
            }
            else
            {
                ret = i + 1;
            }
            break;
        }
    }
    return ret;
}
/** \brief  :获取度目，速度等参数的值，记录在g_fun_info结构体里
 *
 * \param   :con_buf--控制行数据某行的首地址
 *           sys--0，左系统；1，右系统
 *           back--0,前板；1，后板
 *           row--当前控制行的行数，从1开始
 * \return  :none
 * \date    :2015.05.07
 * \author  :wzwz
 */
static void GetLineParam(OPT_FUN *popt, KNIT_PAGE *pcon, int cam, int f0b1, int row)
{
    ushort flw_line = 0;
    ushort gauze = 0;
    switch(cam)
    {
    case 0:
        if(f0b1)
        {
            flw_line = pcon->flw_1B;
        }
        else
        {
            flw_line = pcon->flw_1F;
        }
        gauze = (pcon->gauze1_1 << 8) | pcon->gauze1_9;
        break;
    case 1:
        if(f0b1)
        {
            flw_line = pcon->flw_2B;
        }
        else
        {
            flw_line = pcon->flw_2F;
        }
        gauze = (pcon->gauze2_1 << 8) | pcon->gauze2_9;
        break;
    case 2:
        if(f0b1)
        {
            flw_line = pcon->flw_3B;
        }
        else
        {
            flw_line = pcon->flw_3F;
        }
        gauze = (pcon->gauze3_1 << 8) | pcon->gauze3_9;
        break;
    case 3:
        if(f0b1)
        {
            flw_line = pcon->flw_4B;
        }
        else
        {
            flw_line = pcon->flw_4F;
        }
        gauze = (pcon->gauze4_1 << 8) | pcon->gauze4_9;
        break;
    default:
        return;
    }
    if(flw_line > 0 && flw_line <= GetFlowerHeight(g_knit_data))
    {
        OPT_FUN *tmp = &popt[flw_line - 1];
        if (tmp->con_row == 0)
        {
            tmp->con_row = row;
            tmp->cam = cam;
            //dumu
            if(cam == 1)
            {
                tmp->all.param.dumu = pcon->dumu2;
            }
            else
            {
                tmp->all.param.dumu = pcon->dumu1;
            }
            //speed
            tmp->all.param.speed = pcon->speed;
            //roller
            tmp->all.param.roller = pcon->high_roll;

            tmp->all.param.gauze = GetGauzeValue(gauze);
            tmp->all.param.sink = pcon->sink;
        }
    }
}
/** \brief  :获取控制行1行的参数
 *
 *  \param  :pr--pattern row
 *           con_buf--对应花样行的控制数据，1行
 *  \return :none
 *  \date   :2013\11\29
 *  \author :haoyaoyao
 */
static void GetCntParam (OPT_FUN *popt, KNIT_PAGE *pcon, ushort line)
{
    //sys1, front
    GetLineParam(popt, pcon, 0, 0, line);
    //sys1, back
    GetLineParam(popt, pcon, 0, 1, line);

    //sys2, front
    GetLineParam(popt, pcon, 1, 0, line);
    //sys2, back
    GetLineParam(popt, pcon, 1, 1, line);

    uchar *q = (uchar*)pcon;

    if (q[113] || q[117])
    {
        //压缩的行，放在系统3系统4上
        //sys3, front
        GetLineParam(popt, pcon, 2, 0, line);
        //sys3, back
        GetLineParam(popt, pcon, 2, 1, line);
        //sys4, front
        GetLineParam(popt, pcon, 3, 0, line);
        //sys4, back
        GetLineParam(popt, pcon, 3, 1, line);
    }
}
/*
 *
 */
void GetOptInfo(OPT_FUN *popt)
{
    ushort total = GetTotalKnitPage(g_knit_data);
    KNIT_PAGE *pcon = GetControlData(g_knit_data);
    int i = 0;
    for(i = 0; i < total; ++i)
    {
        GetCntParam(popt, pcon + i, i + 1);
    }
}
/*
 * 去掉字符串中的空格
 */
void Trim(char *pStr)
{
    char *pTmp = pStr;

    while(*pStr != 0)
    {
        if(*pStr != ' ')
        {
            *pTmp++ = *pStr;
        }
        ++pStr;
    }
    *pTmp = 0;
}
